package com.portal.gui.marketman;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.portal.logic.ConnectionManager;
import com.portal.logic.Product;
import com.portal.logic.ProductDAO;
import com.portal.logic.SerializationUtil;
import com.portal.logic.TypeDAO;

public class AddProductScr extends JFrame {

	private JPanel contentPane;
	private JTextField textProductPrice;
	private JTextField textProductDesc;
	private JTextField textProductName;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddProductScr frame = new AddProductScr();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddProductScr() {
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		final JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				Container frame = btnBack.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});
		btnBack.setBounds(45, 201, 97, 25);
		contentPane.add(btnBack);



		JLabel lblProductName = new JLabel("Product Name");
		lblProductName.setBounds(34, 34, 90, 16);
		contentPane.add(lblProductName);

		JLabel lblProductDesc = new JLabel("Product Desc");
		lblProductDesc.setBounds(34, 63, 93, 16);
		contentPane.add(lblProductDesc);

		JLabel lblProductPrice = new JLabel("Product Price");
		lblProductPrice.setBounds(34, 92, 90, 16);
		contentPane.add(lblProductPrice);

		JLabel lblProductType = new JLabel("Product Type");
		lblProductType.setBounds(34, 121, 75, 16);
		contentPane.add(lblProductType);

		final JComboBox cmbTypes = new JComboBox();
		TypeDAO typeDAO = new TypeDAO(ConnectionManager.getConnection());
		List<com.portal.logic.Type> types = typeDAO.selectAll();
		Iterator<com.portal.logic.Type> iterator = types.iterator();
		while (iterator.hasNext()) {
			com.portal.logic.Type type = (com.portal.logic.Type) iterator.next();
			cmbTypes.addItem(type.getName());
		}
		
		cmbTypes.setBounds(136, 118, 90, 22);
		contentPane.add(cmbTypes);

		textProductPrice = new JTextField();
		textProductPrice.setBounds(136, 90, 90, 19);
		contentPane.add(textProductPrice);
		textProductPrice.setColumns(10);

		textProductDesc = new JTextField();
		textProductDesc.setBounds(136, 60, 90, 22);
		contentPane.add(textProductDesc);
		textProductDesc.setColumns(10);

		textProductName = new JTextField();
		textProductName.setBounds(136, 31, 90, 22);
		contentPane.add(textProductName);
		textProductName.setColumns(10);
		
		
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String productName = (String)textProductName.getText();
				String productDesc = (String)textProductDesc.getText();
				String productPrice = (String)textProductPrice.getText();
				String selectedType = (String)cmbTypes.getSelectedItem();
				
				TypeDAO typeDAO = new TypeDAO(ConnectionManager.getConnection());
				com.portal.logic.Type type = typeDAO.selectByName(selectedType);
				
				Product product = new Product();
				ProductDAO productDAO = new ProductDAO(ConnectionManager.getConnection());
				product.setId(productDAO.selectMaxId()+1);
				product.setName(productName);
				product.setDesc(productDesc);
				product.setPrice(Integer.parseInt(productPrice));
				product.setIdType(type.getId());
				
				try {
					SerializationUtil.serialize(product, "c:\\product.out");
				} catch (IOException ioe) {
					ioe.printStackTrace();
				}
				
				productDAO.insert();
				JOptionPane.showMessageDialog(null, "New Product was added!");
			}
		});



		btnSubmit.setBounds(253, 201, 97, 25);
		contentPane.add(btnSubmit);
	}
}
